import java.util.Arrays;
public class SZ2 {

    public static void main6(String[] args) {
        //二封查找法（前提是数据有序）
        int[] array = {1, 2, 8, 6, 15, 42, 55};
        Arrays.sort(array);//将数组数据排序
        System.out.println(Arrays.toString(array));
        int ret = binarySearch(array,15);
        if(ret != -1) {
            System.out.println("找到了，在数组的第"+(ret+1) +"个位置");
        }else {
            System.out.println("找不到！");
        }
    }
        public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while(left <= right) {
            int mid = (left + right) / 2;
            if (array[mid] < key) {
                left = mid + 1;
            } else if (array[mid] == key) {
                return mid;
            }else {
                right = mid - 1;
            }
        }
        return -1;
    }


    static int i= 0;
    public static void main5(String[] args) {
        int[] array = {1, 2, 8, 6, 15, 42};
        int ret = find2(array,6);

        if(ret == i+1){
            System.out.println("找到了,在数组的第"+(i+1)+"个位置");
        }else {
            System.out.println("找不到");
        }
    }
    public static int find2(int[] array,int key) {
        for (  i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return i+1;
            }
        }
        return -1;
    }

static int ret1 = 0;
    public static void main4(String[] args) {
        //顺序查找数组中指定的元素(优化mian3的代码)
        int[] array = {1, 2, 8, 6, 15, 42};
        int ret = find1(array,6);
        System.out.println(ret1);
        if(ret == ret1){
            System.out.println("找到了,在数组的第"+ret1+"个位置");
        }else {
            System.out.println("找不到");
        }
    }
    public static int find1(int[] array,int key) {
        for ( int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                ret1 = i+1;
                return ret1;
            }
        }
        return -1;
    }


    public static void main3(String[] args) {
        //顺序查找数组中指定的元素
        int[] array = {1, 2, 8, 6, 15, 42};
        int ret = find(array,15);
       if(ret == 1){
        System.out.println("找到了");
    }else {
           System.out.println("找不到");
       }
    }
    public static int find(int[] array,int key) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return 1;
            }
        }
            return -1;
    }


    public static void main2(String[] args) {
        //求数组的平均值
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        System.out.println(avg(array));
    }
        public static double avg(int[] array) {
        int sum = 0;
            for (int i:array) {
                sum += i;
            }
            return (double)sum / (double)array.length;
    }


    public static void main1(String[] args) {
        int[] array ={1,2,3,4,5};
        int[] copy = array.clone();
        System.out.println(Arrays.toString(copy));
//        int[] dest = new int[array.length];
//        System.arraycopy(array,0,dest,0,array.length);
//        System.out.println(Arrays.toString(dest));
    }
}
